/*
* $Id: FieldsValueStore.java 292 2011-03-24 21:08:17Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2010 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.db;

import java.io.*;

/**
 * User's fields values database
 * @author lexus
 */
public class FieldsValueStore extends DBStore
{
    /**
     * Container of fields values records
     * @author lexus
     */
    public class FieldsValueRecord extends DBRecord
    {
        /** Identificator user's fields */
        public int idRecord;
        /** Name of user's field */
        public String nameField;
        /** Value of user's field */
        public String valueField;
        /** Type of user's field */
        public int typeField;

        /**
         * Constructor
         */
        public FieldsValueRecord() {
            nameField = new String();
            valueField = new String();
        }

        /**
         * Gets byte array of record
         * @return byte array
         */
        public byte[] getData() {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);
                dos.writeInt(idRecord);
                dos.writeUTF(nameField);
                dos.writeUTF(valueField);
                dos.writeInt(typeField);
                return baos.toByteArray();
            } catch (IOException ex) {}
            return null;
        }

        /**
         * Sets byte array record.
         * Array will be parsed and all fields of it will be filled
         * @param dis data input stream
         */
        public void setData(DataInputStream dis) throws IOException{
                idRecord = dis.readInt();
                nameField = dis.readUTF();
                valueField = dis.readUTF();
                typeField = dis.readInt();
        }
    }

    /**
     * Constructor
     */
    public FieldsValueStore() {
        super("db_fields_value");
    }

    /**
     * Gets new record
     * @return new record
     */
    public DBRecord getRecord() {
        return new FieldsValueRecord();
    }
}
